package com.mlearning.demo.controller;

import com.mlearning.demo.pojo.neo4j.User;
import com.mlearning.demo.service.BookService;
import com.mlearning.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

import com.mlearning.demo.util.EncryptUtil;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private BookService bookService;

    @RequestMapping("/personal_center")
    public String personal_center(HttpServletRequest request,Model model) {
        HttpSession session = request.getSession();
        User user=(User)session.getAttribute("user");
        long days= (new Date().getTime()-user.getRegisterTime()) /1000/60/60/24;
        user.setTrees(userService.getMyTrees(user.getId()));
        model.addAttribute("user", user);
        model.addAttribute("days",days);
        model.addAttribute("recKnow",userService.getRecKnows(user.getId()));
        model.addAttribute("mastered",userService.getMastered(user.getId()));
        model.addAttribute("myBooks",userService.getMyBooks(user.getId()));
        model.addAttribute("recBooks",bookService.getRecBooks(user.getId()));
        return "user/personal_center";
    }
    @RequestMapping("/personal_edit")
    public String edit(HttpServletRequest request,Model model) {
        HttpSession session = request.getSession();
        User user=(User)session.getAttribute("user");
        long days=(new Date().getTime()-user.getRegisterTime())/1000/60/60/24;
        user.setTrees(userService.getMyTrees(user.getId()));
        model.addAttribute("user", user);
        model.addAttribute("days",days);
        model.addAttribute("recKnow",userService.getRecKnows(user.getId()));
        model.addAttribute("masteredQuan",userService.getMasteredQuan(user.getId()));
        model.addAttribute("myBooksQuan",userService.getMyBooksQuan(user.getId()));
        return "user/edit";
    }
}
